const router = require("koa-router")();
const ShopcarModel = require("../../models/shopcar")
// const upload = require("../../config/multer")    //不需要了
const path = require("path");//加
const fileUpload = require("../../utils/upload")
// router.post('/m1/doEdit',upload.single('file'), async (ctx, next) => {
router.post('/m1/doEdit',async ctx=> {
    var {id,name,singer,like,playCount} = ctx.request.body;
    // console.log(ctx.request.body)
    // console.log(ctx.req.file.filename)
    var file = ctx.request.files.file;
    var uploadName = path.basename(file.path);
    var isUpload = Boolean(file.name.trim());
    
    var data = {
        name,
        singer,
        like: Boolean(like),
        playCount,
    }
    
    if(isUpload){
        fileUpload(file.path,uploadName);
        var imgUrl = ctx.origin +"/"+uploadName;
        data.imgUrl = imgUrl
        // await ShopcarModel.updateOne({_id:id},{data})
    }
    await ShopcarModel.updateOne({_id:id},data)
    ctx.redirect("/m1")
    // console.log(id)
}) 
module.exports = router;